package com.probiz.estore.sales.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.sales.PromoRule;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.sales.dao.SalesDashboardDao;

public class SalesDashboardDaoImpl extends HibernateGenericDaoImpl<PromoRule> implements SalesDashboardDao{
	public List<PromoRule> getAllPromotionRulesInProcess(){
		List list;
		list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Date now = new Date();
				StringBuffer sb = new StringBuffer();
				sb.append("from PromoRule r where r.status = ").append(
						Constants.STATUS_ACTIVE).append(" and ");
				sb.append("r.startTime < :now  and ");
				sb.append("(:now < r.endTime or r.endTime is null) ");
				sb.append(" order by r.priority asc,r.promoRuleId asc");
				Query query = session.createQuery(sb.toString());
				query.setTimestamp("now", now);
				List<PromoRule> rows = (List<PromoRule>) query.list();
				
				return rows;
			}

		});
		return list;
	}
}
